#!/bin/bash

echo "========================================"
echo "绘本阅读后端服务启动脚本"
echo "========================================"
echo

echo "[1/4] 检查Java环境..."
if ! command -v java &> /dev/null; then
    echo "错误: 未找到Java环境，请先安装JDK 21"
    exit 1
fi
java -version

echo
echo "[2/4] 检查端口48080是否被占用..."
if lsof -Pi :48080 -sTCP:LISTEN -t >/dev/null ; then
    echo "警告: 端口48080已被占用，请先关闭占用该端口的程序"
    echo "使用以下命令查找并关闭："
    echo "lsof -i :48080"
    echo "kill -9 <PID>"
    read -p "按Enter继续或Ctrl+C退出..."
fi

echo
echo "[3/4] 进入项目目录..."
cd "$(dirname "$0")"
if [ ! -f "pom.xml" ]; then
    echo "错误: 未找到pom.xml文件，请确保在正确的目录下运行此脚本"
    exit 1
fi

echo
echo "[4/4] 启动后端服务..."
echo "正在启动芋道框架后端服务，请稍候..."
echo "服务将在 http://localhost:48080 启动"
echo

mvn spring-boot:run -pl yudao-server -Dspring.profiles.active=local

echo
echo "服务已停止"
